
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class FileBrowser implements CommandListener {

    private static final String UP_DIRECTORY = "..";
    private static final String MEGA_ROOT = "/";
    private static final String SEP_STR = "/";
    private static final char SEP = '/';
    private String currDirName;
    private Command back = new Command("Back", Command.BACK, 2);
    private Command cmdSend = new Command("Send", Command.OK, 2);
    private HomeScreen homeScreen;
    private Image dirIcon;
    private Image fileIcon;
    private byte[] datas;

    public FileBrowser(HomeScreen homeScreen) {
        this.homeScreen = homeScreen;
        currDirName = MEGA_ROOT;
        try {
            dirIcon = Image.createImage("/dir.png");
        } catch (Exception e) {
            dirIcon = null;
        }
        try {
            fileIcon = Image.createImage("/file.png");
        } catch (Exception e) {
            fileIcon = null;
        }
    }

    public void ShowCurrDir() {
        Enumeration e;
        FileConnection currDir = null;
        List browser;
        try {
            if (MEGA_ROOT.equals(currDirName)) {
                e = FileSystemRegistry.listRoots();
                browser = new List(currDirName, List.IMPLICIT);
            } else {
                currDir = (FileConnection) Connector.open("file://localhost/" + currDirName);
                e = currDir.list();
                browser = new List(currDirName, List.IMPLICIT);
                browser.append(UP_DIRECTORY, dirIcon);
            }
            while (e.hasMoreElements()) {
                String fileName = (String) e.nextElement();
                if (fileName.charAt(fileName.length() - 1) == SEP) {
                    browser.append(fileName, dirIcon);
                } else {
                    browser.append(fileName, fileIcon);
                }
            }
            browser.setCommandListener(this);
            browser.addCommand(back);
            if (currDir != null) {
                currDir.close();
            }
            homeScreen.display.setCurrent(browser);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == back) {
            if (currDirName.equals(MEGA_ROOT)) {
                homeScreen.DisplayChoseViewAlbumForm();
            } else {
                ShowCurrDir();
            }
        } else if (c == List.SELECT_COMMAND) {
            List browser = (List) d;
            final String currFile = browser.getString(browser.getSelectedIndex());
            new Thread(new Runnable() {

                public void run() {
                    if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) {
                        TraverseDirectory(currFile);
                    } else {
                        try {
                            datas = FileUtil.ReadFile("file://localhost/" + currDirName + currFile);
                            ViewImage(datas);
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }).start();
        } else if (c == cmdSend) {
            try {

                Common.Client.SendImage(datas);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private void ViewImage(byte[] datas) {
        ViewImage viewImage = new ViewImage(datas);
        viewImage.addCommand(back);
        viewImage.addCommand(cmdSend);
        viewImage.setCommandListener(this);
        homeScreen.display.setCurrent(viewImage);
    }

    public void TraverseDirectory(String fileName) {
        if (currDirName.equals(MEGA_ROOT)) {
            if (fileName.equals(UP_DIRECTORY)) {
                return;
            }
            currDirName = fileName;
        } else if (fileName.equals(UP_DIRECTORY)) {
            int i = currDirName.lastIndexOf(SEP, currDirName.length() - 2);
            if (i != -1) {
                currDirName = currDirName.substring(0, i + 1);
            } else {
                currDirName = MEGA_ROOT;
            }
        } else {
            currDirName = currDirName + fileName;
        }
        ShowCurrDir();
    }
}
